﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Collections;

namespace DALL.SysManage
{
    public class T_PUB_BROADCAST
    {
        public T_PUB_BROADCAST()
        { }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        //public Model.SysManage.T_PUB_BROADCAST GetModelByBranch(int branch_id)
        //{

        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select ID,BRANCH_ID,MODULE,FILE_ID,FILE_TITEL from T_PUB_BROADCAST ");
        //    strSql.Append(" where BRANCH_ID=@BRANCH_ID ");
        //    Database db = DatabaseFactory.CreateDatabase();
        //    DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
        //    db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, branch_id);
        //    Model.SysManage.T_PUB_BROADCAST model = null;
        //    using (IDataReader dataReader = db.ExecuteReader(dbCommand))
        //    {
        //        if (dataReader.Read())
        //        {
        //            model = ReaderBind(dataReader);
        //        }
        //    }
        //    return model;
        //}


        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetBroadcastList(string strWhere)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select ID,a.BRANCH_ID,MODULE,FILE_ID,FILE_TITEL,BRANCH_NAME ");
        //    strSql.Append(" FROM T_PUB_BROADCAST as a, T_PUB_BRANCH as c");
        //    strSql.Append(" where c.BRANCH_ID = a.BRANCH_ID");
        //    if (strWhere.Trim() != "")
        //    {
        //        strSql.Append(strWhere);
        //    }
        //    Database db = DatabaseFactory.CreateDatabase();
        //    return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        //}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetBroadcastList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT(a.BRANCH_ID),BRANCH_NAME,MODULE,FILE_TITEL ");
            strSql.Append(" FROM T_PUB_BROADCAST as a, T_PUB_BRANCH as c");
            strSql.Append(" where c.BRANCH_ID = a.BRANCH_ID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void UpdateData(IList<Model.SysManage.T_PUB_BROADCAST> modelList)
        {
            ArrayList strArry = new ArrayList();
            foreach (Model.SysManage.T_PUB_BROADCAST item in modelList)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update T_PUB_BROADCAST set ");
                if (item.BRANCH_ID != null)
                {
                    strSql.Append("BRANCH_ID=" + item.BRANCH_ID + ",");
                }
                else
                {
                    strSql.Append("BRANCH_ID= null ,");
                }
                if (item.MODULE != null)
                {
                    strSql.Append("MODULE='" + item.MODULE + "',");
                }
                else
                {
                    strSql.Append("MODULE= null ,");
                }
                if (item.FILE_ID != null)
                {
                    strSql.Append("FILE_ID='" + item.FILE_ID + "',");
                }
                else
                {
                    strSql.Append("FILE_ID= null ,");
                }
                if (item.FILE_TITEL != null)
                {
                    strSql.Append("FILE_TITEL='" + item.FILE_TITEL + "',");
                }
                else
                {
                    strSql.Append("FILE_TITEL= null ,");
                }
                int n = strSql.ToString().LastIndexOf(",");
                strSql.Remove(n, 1);
                strSql.Append(" where ID=" + item.ID + "");
                strArry.Add(strSql);
            }

            DBHelper.ExecuteSqlTran(strArry);
        }



        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void AddData(IList<Model.SysManage.T_PUB_BROADCAST> modelList)
        {
            ArrayList strArry = new ArrayList();
            foreach (Model.SysManage.T_PUB_BROADCAST item in modelList)
            {
                StringBuilder strSql = new StringBuilder();
                StringBuilder strSql1 = new StringBuilder();
                StringBuilder strSql2 = new StringBuilder();
                if (item.BRANCH_ID != null)
                {
                    strSql1.Append("BRANCH_ID,");
                    strSql2.Append("" + item.BRANCH_ID + ",");
                }
                if (item.MODULE != null)
                {
                    strSql1.Append("MODULE,");
                    strSql2.Append("'" + item.MODULE + "',");
                }
                if (item.FILE_ID != null)
                {
                    strSql1.Append("FILE_ID,");
                    strSql2.Append("'" + item.FILE_ID + "',");
                }
                if (item.FILE_TITEL != null)
                {
                    strSql1.Append("FILE_TITEL,");
                    strSql2.Append("'" + item.FILE_TITEL + "',");
                }
                strSql.Append("insert into T_PUB_BROADCAST(");
                strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
                strSql.Append(")");
                strSql.Append(" values (");
                strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
                strSql.Append(")");
                strArry.Add(strSql);
            }

            DBHelper.ExecuteSqlTran(strArry);
        }


        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_BROADCAST model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_BROADCAST(");
            strSql.Append("BRANCH_ID,MODULE,FILE_ID,FILE_TITEL)");

            strSql.Append(" values (");
            strSql.Append("@BRANCH_ID,@MODULE,@FILE_ID,@FILE_TITEL)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "MODULE", DbType.String, model.MODULE);
            db.AddInParameter(dbCommand, "FILE_ID", DbType.String, model.FILE_ID);
            db.AddInParameter(dbCommand, "FILE_TITEL", DbType.String, model.FILE_TITEL);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysManage.T_PUB_BROADCAST model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_BROADCAST set ");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("MODULE=@MODULE,");
            strSql.Append("FILE_ID=@FILE_ID,");
            strSql.Append("FILE_TITEL=@FILE_TITEL");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "MODULE", DbType.String, model.MODULE);
            db.AddInParameter(dbCommand, "FILE_ID", DbType.String, model.FILE_ID);
            db.AddInParameter(dbCommand, "FILE_TITEL", DbType.String, model.FILE_TITEL);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_BROADCAST ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_BROADCAST GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,MODULE,FILE_ID,FILE_TITEL from T_PUB_BROADCAST ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            Model.SysManage.T_PUB_BROADCAST model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,MODULE,FILE_ID,FILE_TITEL ");
            strSql.Append(" FROM T_PUB_BROADCAST ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_BROADCAST> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,BRANCH_ID,MODULE,FILE_ID,FILE_TITEL ");
            strSql.Append(" FROM T_PUB_BROADCAST ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_BROADCAST> list = new List<Model.SysManage.T_PUB_BROADCAST>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_BROADCAST ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_BROADCAST model = new Model.SysManage.T_PUB_BROADCAST();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            model.MODULE = dataReader["MODULE"].ToString();
            model.FILE_ID = dataReader["FILE_ID"].ToString();
            model.FILE_TITEL = dataReader["FILE_TITEL"].ToString();
            return model;
        }

        #endregion  Method
    }
}
